<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%;">
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <!-- 部门负责人 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <!-- 对部门的操作 -->
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <template v-if="!nodeData.root">
                <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                <el-dropdown-item command="del">删除部门</el-dropdown-item>
              </template>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>
<script>
import { reqDelDepartments } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    nodeData: {
      type: Object,
      required: true
    }
  },
  methods: {
    handleCommand(type) {
      if (type === 'add') {
      // 添加子部门,打开弹窗 , 把当前部门的id传递过去
        this.$emit('on-open', {
          id: this.nodeData.id,
          type: 'add'
        })
      } else if (type === 'edit') {
      // 编辑部门
        this.$emit('on-open', {
          id: this.nodeData.id,
          type: 'edit'
        })
      } else if (type === 'del') {
      // 删除部门
        this.handleDelete(this.nodeData.id)
      }
    },
    async handleDelete(id) {
      const res = await this.$confirm('确定删除该部门吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 如果点击的是取消,什么也不做
      if (res === 'cancel') return
      // 如果点击的是确定,删除该部门
      const ret = await reqDelDepartments(id)
      if (ret.code === 10000) {
        // 提示一下,删除成功 , 刷新列表
        this.$message.success('删除成功')
        // 子派发父监听-->让父组件刷新数据列表
        this.$emit('on-success')
      }
    }
  }
}
</script>
